if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","xmlfile/hotel.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

x=xmlDoc.getElementsByTagName("note");
i=0;

var mapsearch;
function initializeMap(divid) {
if (GBrowserIsCompatible()) {
    mapsearch = new GMap2(document.getElementById(divid));
    mapsearch.setCenter(new GLatLng(10.82313524364468, 106.62963151931763), 15);
     GEvent.addListener(mapsearch, "click", function(overlay,latlng) {
         
      if (latlng) {
           mapsearch.clearOverlays(); 
           //mapsearch.addOverlay(new GMarker(latlng));
           
           switch(divid){
               case 'frmsearchhotel_canvas':
               $("#hdlatsearchhotel").val(latlng.lat());
               $("#hdlngsearchhotel").val(latlng.lng());
               break;
               default: break;                   
           }
           distance = $("#hddistance").val();
           alertDistance(distance);
      }
      
      
     });
var customUI = mapsearch.getDefaultUI();
customUI.maptypes.hybrid = false;
mapsearch.setUI(customUI);

}
}

 function createMarkerSearch(lat,lng,id,name) {
      latlng = new GLatLng(lat, lng);
      
       var baseIcon4 = new GIcon(G_DEFAULT_ICON);
    var icn2 = new GIcon(baseIcon4);    
    icn2.image = 'images/ok_save.png';
    var opts2 =
    {
        icon: icn2,
        clickable: true
    };
    var marker = new GMarker(latlng,opts2);
      marker.value = id;
      GEvent.addListener(marker,"mouseover", function(latlng) {
        var myHtml = "<b>#" + id +':<a href="http://vnexpress.net/"> vnexpress</a>'+name + "</b><br/>";
        mapsearch.openInfoWindowHtml(latlng,myHtml);
      });
//       GEvent.addListener(marker,"click", function() {
//           var myHtml = "<b>#" + id +':'+name + "</b><br/>";
//        mapsearch.openInfoWindowHtml(latlng,myHtml);
//      });
      return marker;
    }

function alertDistance(distance){
    lat1 = $('#hdlatsearchhotel').val();
    lng1 = $('#hdlngsearchhotel').val();
    latlng = new GLatLng(lat1,lng1);    
    var baseIcon4 = new GIcon(G_DEFAULT_ICON);
    var icn2 = new GIcon(baseIcon4);    
    icn2.image = 'http://l.f1.img.vnexpress.net/2012/06/22/ronaldo1-1340318647_480x0.jpg';
    var opts2 =
    {
        icon: icn2,
        clickable: true
    };
    var marker = new GMarker(latlng,opts2);
    mapsearch.addOverlay(marker);
    for(i=0;i<x.length;i++){
        lat2 = x[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue;
        lng2 = x[i].getElementsByTagName("lng")[0].childNodes[0].nodeValue;
        id = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
        name = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
        if(calDistance(lat1,lng1,lat2,lng2,distance)){
            mapsearch.addOverlay(createMarkerSearch(lat2,lng2, id,name));
        }
    }
}

function calDistance(lat1, lon1, lat2, lon2,distance) {
    var R = 6371; // Radius of earth in Miles 
    var dLat = toRad(lat2-lat1);
    var dLon = toRad(lon2-lon1); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    if(d<=distance){
        return true ;   
    }
    
    return false;
}

function toRad(Value) {
    /** Converts numeric degrees to radians */
    return Value * Math.PI / 180;
}

